package com.qd.pandaparkflow.compment.charge;

import com.qd.pandaparkflow.bean.ChargeFeeParkTimeReqVO;
import com.qd.pandaparkflow.context.ChargeFeeParkTimeContext;
import com.yomahub.liteflow.core.NodeComponent;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.stereotype.Component;

import java.time.Duration;
import java.util.ArrayList;

/**
 * 初始化组件
 * @author sjk
 */
@ConditionalOnProperty(prefix = "liteflow", name = "enable", havingValue = "true")
@Component("feeTimeInitCmp")
public class FeeTimeInitComponent extends NodeComponent {
    @Override
    public void process() throws Exception {
        ChargeFeeParkTimeReqVO req = this.getRequestData();
        ChargeFeeParkTimeContext context = this.getContextBean(ChargeFeeParkTimeContext.class);
        context.setStartTime(req.getStartTime());
        context.setEndTime(req.getEndTime());
        context.setAmount(req.getAmount());
        context.setFeeTimeId(req.getFeeTimeId());
        context.setFeeTimeRules(req.getFeeTimeRules());
        context.setPrintLog(new ArrayList<>(16));
        if(null!=req.getStartTime() && null!=req.getEndTime()){
            final Duration duration = Duration.between(req.getStartTime(), req.getEndTime());
            final long minutes = duration.toMinutes();
            context.setMinutes(minutes);
        }
    }

    @Override
    public boolean isAccess() {
        ChargeFeeParkTimeReqVO req = this.getSlot().getRequestData();
        return req != null;
    }
}
